package me.leon.ade;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import org.junit.Test;

public class ChareDect {

    private static FileWriter fileWriter;

    public static String getEncoding(String str) {
        String encode;

        encode = "UTF-16";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "ASCII";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return "字符" + str + " >>中仅由数字和英文字母组成，无法识别其编码格式";
            }
        } catch (Exception ex) {
        }

        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        return "unknown";
    }

    /**
     * 字符串转换unicode
     *
     * @param string
     * @return
     */
    public static String stringToUnicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            unicode.append("\\u" + Integer.toHexString(string.charAt(i)));
        }
        return unicode.toString();
    }

    /**
     * unicode 转字符串
     *
     * @param unicode
     * @return
     */
    public static String unicodeToString(String unicode) {
        StringBuilder string = new StringBuilder();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            string.append((char) Integer.parseInt(hex[i], 16));
        }
        return string.toString();
    }

    @Test
    public void prop() {
        Properties properties = new Properties();
        FileReader reader = null;
        String path = "d.properties";
        try {
            boolean exists = new File(path).exists();
            if (exists) {
                reader = new FileReader(path);
                properties.load(reader);
            }
            if (!exists) {
                for (int i = 0; i < 4; i++) {
                    properties.setProperty(
                            "thread" + i, String.valueOf(System.currentTimeMillis() / 1000));
                }
            }
            {
                for (int i = 0; i < 4; i++) {
                    System.out.println(Long.parseLong(properties.getProperty("thread" + i)));
                }
            }

            properties.store(new FileWriter(path), "multi thread download process");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
